// pages/uploadimg/uploadimg.js
const app = getApp();
var ea = require('../../utils/each.js');
const qiniuUploader = require('../../utils/qiniuUploader.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    delete: app.data.imgurl +"delete.png",
    checkeds:false,
    add: app.data.imgurl +"adds.png",
    upcase: [],
    array:["风景","人物","时光"],
    index:0,
    rightarrow: app.data.imgurl +"rightarrow.png",
    items: [{ name: '可下载', checked: false ,id:1}, { name: '不可下载', checked:true,id:0}],
    bisai:0,
    shuju: { xiazai:0,tupian:[]},
    getclass: app.data.url +'sheyingjia/Xzuoping1/getclasslist',
    getqiniutioken: app.data.url + 'sheyingjia/Xuploas1/gettoken',
    addenclosure: app.data.url + 'sheyingjia/Xuploas1/addenclosure',
    addtupianzuoping: app.data.url + 'sheyingjia/Xzuoping1/addtupianzuoping',
  },
  //选择分类
  bindPickerChange:function(e){
    console.log(e.detail.value)
    var array = this.data.array;
    var index = e.detail.value;
    var shuju = this.data.shuju;
    shuju.fenlei = array[index].id;
    console.log(shuju);
    this.setData({
          index:index,
          shuju:shuju
    })
  },
  yuedu:function(e){
    var checkeds = this.data.checkeds;
    if (checkeds){
      checkeds = false;
    }else{
      checkeds = true;
    }
    console.log(checkeds);
    this.setData({ checkeds: checkeds});
  },
  //添加数据
  tianjia:function(e){
    // console.log(e);
    var str = e.detail.value;
    var type = e.currentTarget.dataset.type;
    var shuju = this.data.shuju;
    shuju[type] = str;
    console.log(shuju);
    this.setData({shuju:shuju});
  },
  //选择是否可下载
  radioChange:function(e){
    var items = this.data.items;
    var newitems = [];
    var shuju = this.data.shuju;
    ea.each(items,function(i,v){
      if(v.checked){
        v.checked = false;
      }else{
        shuju.xiazai = v.id;
        v.checked = true;
      }
      newitems.push(v);
    })
    console.log(shuju);

    this.setData({items:newitems,shuju:shuju});
  },
 //  选择图片
  chooseImage:function(){
    var that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        if (res.tempFilePaths.count == 0) {
          return;
        }
        var upcase = that.data.upcase;
        if ((upcase.length + res.tempFilePaths.count)>9){
          wx.showToast({
            title: '最多上传9张',
            icon:'none'
          })
          return;
        }
        wx.showLoading({
          title: '上传中',
        })
        ea.upfile(res.tempFilePaths,function(res,key){
          console.log(res);
          console.log(key);
          ea.request(that.data.addenclosure,{name:key},function(ress){
            wx.hideLoading();
            var data  = ress.data;
            if(data.code==200){
              // console.log(data.data);
              var shuju = that.data.shuju;
              ea.each(data.data,function(i,v){
                if(upcase.length==0&&i==0){
                  v.fenmian = true;
                  shuju.fenmian = v.id;
                }else{
                  v.fenlei = false;
                  shuju.tupian.push(v.id);
                }
                upcase.push(v);
              
              })
              console.log(shuju);
              that.setData({ upcase: upcase ,shuju:shuju});
            }else{
              wx.showToast({
                title: data.msg,
                icon:'none'
              })
            }
          })
        }, that.data.getqiniutioken,'SCN');
      }
    })
  },
  //设置封面
  shezfenmian:function(e){
    // console.log(e)
    var id = e.currentTarget.dataset.id;
    var upcase = this.data.upcase;
    var shuju = this.data.shuju;
    var newupcase = [];
    var newshuju = [];
    ea.each(upcase,function(i,v){
      if(v.id==id){
        v.fenmian = true;
        shuju.fenmian = v.id;
      }else{
        newshuju.push(v.id);
        v.fenmian = false;
      }
      newupcase.push(v);
    })
    shuju.tupian = newshuju;
    console.log(shuju);
    this.setData({ upcase:newupcase,shuju:shuju});

  },
  //删除图片
  delet:function(e){
  var  upcase=this.data.upcase;
    var index = e.currentTarget.dataset.index;
    var shuju = this.data.shuju;
    var newshuju = [];
    var newupcase = [];
    var youfenmian = false;
    if (upcase.length<=1){
      wx.showToast({
        title: '最少要有一个封面',
        icon:'none'
      })
      return;
    }
    ea.each(upcase,function(i,v){
      if (v.id == index){
        if (v.fenmian) {
          youfenmian = true;
        }
      }else{
        newupcase.push(v);
      }
    })
  
    if (youfenmian){
      newupcase[0].fenmian = true;
    }
    ea.each(newupcase,function(i,v){
      if(v.fenmian){
        shuju.fenmian = v.id;
      }else{
        newshuju.push(v.id);
      }
    })
    shuju.tupian = newshuju;
    console.log(shuju)
    // upcase.splice(index,1)
  this.setData({
    upcase: newupcase,
    shuju:shuju
  })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bisai = options.bisai;
    console.log(bisai);
    var shuju = this.data.shuju;
    if(bisai){
      shuju.bisai = bisai;
      this.setData({ bisai: bisai, shuju: shuju});
    }else{
      shuju.bisai = 0;
      this.setData({ bisai: 0, shuju: shuju});
    }
    var that = this;
    ea.request(that.data.getclass,{bisai:that.data.bisai},function(res){
      var data = res.data;
      console.log(data);
      if(data.code==200){
        that.setData({array:data.data});
        var shuju = that.data.shuju;
        shuju.fenlei = data.data[0].id;
        that.setData({shuju:shuju});
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    });
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.model);
        var shuju = that.data.shuju;
        shuju.zhuangbei = res.model;
        console.log(shuju);
        that.setData({ shuju: shuju });
      },
    })
  },
  //上传数据
  fabuzuoping:function(){
    var shuju = this.data.shuju;
    var user = wx.getStorageSync('user');
    var checkeds = this.data.checkeds;
    var that = this;
    if (!checkeds){
      wx.showToast({
        title: '请先点击阅读',
        icon:'none'
      })
      return;
    }
    if(!user){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return
    }
    shuju.token = user.token;
    wx.showLoading({
      title: '上传中',
    })
    ea.request(that.data.addtupianzuoping,shuju,function(res){
      wx.hideLoading();
      var data  = res.data;
      console.log(data);
      // return;
      if(data.code==200){
        wx.showToast({
          title: data.msg,
        })
        setTimeout(function(){
          wx.navigateBack({
            delta:1,
            fail:function(){
              wx.switchTab({
                url: '/pages/upload/upload',
              })
            }
          })
        },app.data.times);
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})